package com.project.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.AOP.HasRole;
import com.project.common.R;
import com.project.dto.AnnouncementDto;
import com.project.dtoplus.AnnouncementDtoPlus;
import com.project.entity.Annex;
import com.project.service.AnnexService;
import com.project.service.AnnouncementService;
import com.project.utils.AliOSSUtils;
import com.project.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/announcement")
public class AnnouncementController {


    @Autowired
    private AnnouncementService announcementService;
    @Autowired
    private AliOSSUtils aliOSSUtils;

    @Autowired
    private AnnexService annexService;

    @Autowired
    private RedisTemplate redisTemplate;

    //1.添加公告
    @HasRole(2)
    @PostMapping("/add")
    public R<String> addNotice( @RequestBody AnnouncementDto announcementDto) {

       announcementService.saveWithAnnex(announcementDto);

        return R.success("上传成功");

    }

    //添加附件
    //添加公告附件
    @PostMapping("/upload")
    public R<List<Annex>> upload(@RequestPart MultipartFile[] files)throws IOException {
        List<Annex> annexes = announcementService.addAnnex(files);
        return R.success(annexes);
    }


    //2.修改公告时信息回填
    @GetMapping("/{id}")
    public R<AnnouncementDtoPlus> getById(@PathVariable Long id){
        AnnouncementDtoPlus byIdWithAnnex = announcementService.getByIdWithAnnex(id);

        return R.success(byIdWithAnnex);
    }

    //3.修改公告
    @HasRole(2)
    @PutMapping
    public R<String> update(@RequestBody   AnnouncementDto announcementDto) throws IOException {

        announcementService.updateWithAnnex(announcementDto);

        return R.success("修改成功！");
    }

    //4.删除公告
    @DeleteMapping
    public R<String> deleteById( String[] ids){
        announcementService.deleteAnnex(ids);
        return R.success("删除成功");
    }

    //5。分页查询获取公告
    @GetMapping("/page")
    public R<Page>  page (int page, int pageSize,@RequestParam String title,@RequestParam String department ,@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate modifiedTime){
      Page announcementDtoPage=  announcementService.pageSearch( page, pageSize,  title, department ,   modifiedTime);

        return  R.success(announcementDtoPage);
    }

    //6.分页查询最新四条数据公告
    @GetMapping("/getLatestNotice")
    public  R<Page> pageFour(int page,int pageSize){

        Page lastFour = announcementService.searchLastFour(page, pageSize);
        return R.success(lastFour);
    }

    //7.查看详情
    @GetMapping("/getDetail")
    public R<AnnouncementDtoPlus> getDetail( Long id){
        AnnouncementDtoPlus detail = announcementService.getDetail(id);
//        String content = detail.getContent();
        return R.success(detail);
    }





    //9.下载公告
    @GetMapping("/download")
    public R<String> download(@RequestParam String url) throws IOException {

      //  String s = announcementService.downloadAnnex(id,name);
        aliOSSUtils.download(url);
       return R.success("下载成功");
    }




    }


